package com.project.class8_15.dao.impl;

import java.lang.reflect.Field;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {
    protected Connection con;
    protected PreparedStatement ps;
    protected ResultSet rs;

    public void setConnection(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:12345/mydb?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false"
            ,"root","lovo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeConnection(){
        try {
            if(rs != null) {
                rs.close();
            }
            if(ps != null) {
                ps.close();
            }
            if(con != null) {
                con.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 更新数据库数据
     * @param sql 执行的SQL语句
     * @param valueArray 占位符数组
     */
    public void updateData(String sql,Object... valueArray){
        this.setConnection();

        try {
            ps = con.prepareStatement(sql);
            for(int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);
            }

            ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    public List find(String sql,Class beanClass,Object... valueArray){
        this.setConnection();
        List list = new ArrayList();

        try {
            ps = con.prepareStatement(sql);
            for(int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);
            }

//            //得到属性列表
//            Field[] fs = beanClass.getDeclaredFields();
//            rs = ps.executeQuery();
//            while(rs.next()){
//                //调用无参构造方法，产生对象
//                Object obj = beanClass.getConstructor().newInstance();
//
//                for(Field f : fs){
//                    try {
//                        //根据列名，从结果集中得到数据
//                        Object value = rs.getObject(f.getName());
//                        if (value instanceof java.sql.Date) {
//                            value = LocalDate.parse(value.toString());
//                        }
//                        //去除访问修饰符的检查
//                        f.setAccessible(true);
//                        //将obj对象中的指定属性，填充value值
//                        f.set(obj, value);
//                    }catch (Exception e){
//                        continue;
//                    }
//                }
//                list.add(obj);
//            }

            rs = ps.executeQuery();
            //得到结果集信息对象
            ResultSetMetaData rm = rs.getMetaData();
            while(rs.next()){
                Object obj = beanClass.getConstructor().newInstance();
                //rm.getColumnCount()得到查询列的个数
                for(int i=1;i<=rm.getColumnCount();i++){
                    //得到当前列的列名
                    String columnName = rm.getColumnName(i);
                    //得到指定列的数据
                    Object value = rs.getObject(columnName);
                    if(value instanceof Date){
                        value = LocalDate.parse(value.toString());
                    }

                    //根据列名，得到属性对象
                    Field f = beanClass.getDeclaredField(columnName);

                    f.setAccessible(true);
                    f.set(obj,value);
                }
                list.add(obj);
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }

        return list;
    }
}
